#include "searcher.hpp"
#include "third_lib/httplib"
#include<memory>
#include"Log.hpp"

const std::string input = "data/output/dest.txt";//从input里读取数据构建索引
const std::string root_path = "./wwwroot";
int main()
{
        std::unique_ptr<ns_searcher::Searcher> searcher(new ns_searcher::Searcher());
        searcher->SearcherInit(input);
        httplib::Server svr;
        svr.set_base_dir(root_path.c_str()); // 设置根目录
                                             // 重定向到首页
        svr.Get("/", [](const httplib::Request &, httplib::Response &rsp)
                { rsp.set_redirect("/home/LZF/boost_searcher_project/wwwroot/index.html"); }); // 重定向到首页
        svr.Get("/s",[&searcher](const httplib::Request &req,httplib::Response &rsp)
        {
                if(!req.has_param("word"))
                {
                        rsp.set_content("无搜索关键字!","test/plain,charset=utf-8");
                        return;
                }
                std::string json_str;
                std::string query = req.get_param_value("word");
                std::cout<<"用户正在搜索: "<<query<<std::endl;
                searcher->Search(query,&json_str);
                rsp.set_content(json_str,"application/json");
        });

        svr.listen("0.0.0.0", 8800);
}




//  svr.Get("/nihao", [](const httplib::Request &req, httplib::Response &rsp)
            // { rsp.set_content("Hello World!", "text/plain;charset=utf-8"); });

    // svr.Get("/", [](const httplib::Request &, httplib::Response &rsp)
    //         {
    //             std::ifstream file("./wwwroot/index.html"); // 打开index.html文件
    //             if (!file)
    //             {
    //                 rsp.status = 404;
    //                 rsp.set_content("Not Found", "text/plain");
    //                 return;
    //             }
    //             std::string content((std::istreambuf_iterator<char>(file)),
    //                                 std::istreambuf_iterator<char>()); // 读取文件内容
    //             rsp.set_content(content, "text/html");                 // 将读取内容作为响应的内容，并设置响应的Content-Type为text/html
    //         });
